package com.ewzj.bigdata.mvp.ui.fragment;

import android.content.Intent;
import android.os.Bundle;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.TextView;

import com.bumptech.glide.load.resource.bitmap.CircleCrop;
import com.ewzj.bigdata.R;
import com.ewzj.bigdata.app.constants.AppConstants;
import com.ewzj.bigdata.app.utils.Apputils;
import com.ewzj.bigdata.di.component.DaggerMineComponent;
import com.ewzj.bigdata.di.module.MineModule;
import com.ewzj.bigdata.mvp.contract.MineContract;
import com.ewzj.bigdata.mvp.model.entity.resbean.UserBean;
import com.ewzj.bigdata.mvp.presenter.MinePresenter;
import com.ewzj.bigdata.mvp.ui.activity.EditProfileActivity;
import com.ewzj.bigdata.mvp.ui.activity.FeedBackActivity;
import com.ewzj.bigdata.mvp.ui.activity.LoginActivity;
import com.ewzj.bigdata.mvp.ui.activity.MessageActivityActivity;
import com.ewzj.bigdata.mvp.ui.activity.SettingActivity;
import com.jess.arms.base.BaseFragment;
import com.jess.arms.di.component.AppComponent;
import com.jess.arms.http.imageloader.glide.ImageConfigImpl;
import com.jess.arms.utils.ArmsUtils;
import com.jess.arms.utils.DeviceUtils;

import butterknife.BindView;
import butterknife.OnClick;

import static com.jess.arms.utils.Preconditions.checkNotNull;


public class MineFragment extends BaseFragment<MinePresenter> implements MineContract.View {


    @BindView(R.id.tv_name)
    TextView tvName;
    @BindView(R.id.iv_avatar)
    ImageView ivAvatar;
    @BindView(R.id.view_msg)
    View viewMsg;
    @BindView(R.id.tv_phone)
    TextView tvPhone;
    @BindView(R.id.tv_desc)
    TextView tvDesc;
    private UserBean userBean;

    public static MineFragment newInstance() {
        MineFragment fragment = new MineFragment();
        return fragment;
    }

    @Override
    public void setupFragmentComponent(AppComponent appComponent) {
        DaggerMineComponent //如找不到该类,请编译一下项目
                .builder()
                .appComponent(appComponent)
                .mineModule(new MineModule(this))
                .build()
                .inject(this);
    }

    @Override
    public View initView(LayoutInflater inflater, ViewGroup container, Bundle savedInstanceState) {
        return inflater.inflate(R.layout.fragment_mine, container, false);
    }

    @Override
    public void initData(Bundle savedInstanceState) {

    }

    /**
     * 此方法是让外部调用使fragment做一些操作的,比如说外部的activity想让fragment对象执行一些方法,
     * 建议在有多个需要让外界调用的方法时,统一传Message,通过what字段,来区分不同的方法,在setData
     * 方法中就可以switch做不同的操作,这样就可以用统一的入口方法做不同的事
     * <p>
     * 使用此方法时请注意调用时fragment的生命周期,如果调用此setData方法时onCreate还没执行
     * setData里却调用了presenter的方法时,是会报空的,因为dagger注入是在onCreated方法中执行的,然后才创建的presenter
     * 如果要做一些初始化操作,可以不必让外部调setData,在initData中初始化就可以了
     *
     * @param data
     */

    @Override
    public void setData(Object data) {

    }


    @Override
    public void showLoading() {

    }

    @Override
    public void hideLoading() {

    }

    @Override
    public void showMessage(@NonNull String message) {
        checkNotNull(message);
        ArmsUtils.snackbarText(message);
    }

    @Override
    public void launchActivity(@NonNull Intent intent) {
        checkNotNull(intent);
        ArmsUtils.startActivity(intent);
    }

    @Override
    public void killMyself() {

    }


    @OnClick({R.id.rl_info, R.id.rl_msg, R.id.rl_feedback, R.id.rl_connect, R.id.rl_about, R.id.rl_setting})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.rl_info:
                if (Apputils.isLogin(getActivity())) {
                    Intent intent = new Intent(getActivity(), EditProfileActivity.class);
                    intent.putExtra(AppConstants.EXTRA_DATA, userBean);
                    ArmsUtils.startActivity(intent);
                } else {
                    ArmsUtils.startActivity(LoginActivity.class);
                }
                break;
            case R.id.rl_msg:
                ArmsUtils.startActivity(MessageActivityActivity.class);
                break;
            case R.id.rl_feedback:
                ArmsUtils.startActivity(FeedBackActivity.class);
                break;
            case R.id.rl_connect:
                DeviceUtils.openDial(getActivity(), tvPhone.getText().toString());
                break;
            case R.id.rl_about:
                ArmsUtils.snackbarText("关于我们");
                break;
            case R.id.rl_setting:
                ArmsUtils.startActivity(SettingActivity.class);
                break;
        }
    }

    @Override
    public void onResume() {
        super.onResume();
        if (Apputils.isLogin(getActivity())) {
            mPresenter.loaduserinfo();
        }
    }

    @Override
    public void showUserInfo(UserBean userBean) {
        this.userBean = userBean;
        if (!TextUtils.isEmpty(userBean.getAvatar())) {
            ArmsUtils.obtainAppComponentFromContext(getActivity()).imageLoader().loadImage(getActivity(),
                    ImageConfigImpl.builder().imageView(ivAvatar)
                            .transformation(new CircleCrop())
                            .errorPic(R.drawable.icon_head_default)
                            .placeholder(R.drawable.icon_head_default)
                            .url(Apputils.getFromatUrl(userBean.getAvatar())).build());
        }
        tvName.setText(TextUtils.isEmpty(userBean.getNickname()) ? getString(R.string.userdefault_name) : userBean.getNickname());
        tvDesc.setText(TextUtils.isEmpty(userBean.getSign()) ? getString(R.string.usrdefault_sign) : userBean.getSign());
    }
}
